//Page Object
import regeneratorRuntimer from '../../lib/runtime/runtime'
import { request } from '../../request/index.js'
import { showToast } from "../../utils/asyncWx.js"
const md5 = require('../../utils/md5.js');
Page({
    data: {
        flag: true,
    },
    userKey: {
        loginmsg: "",
        credential: ""
    },
    // islogin用来判断是否登录
    isLogin: "",
    // 用户名
    loginmsg: "",
    // 密码
    credential: "",
    //船舶id

    onLoad(e) {
        this.id = e.id
        this.checkout()
    },
    // 获取输入的用户账号
    handleUser(e) {
        this.loginmsg = e.detail.value;
        console.log(this.loginmsg)
        this.userKey.loginmsg = this.loginmsg
        console.log(this.userKey.loginmsg)

    },
    passwordValue(e) {
        this.credential = e.detail.value
        console.log(this.credential)
        this.userKey.credential = md5.hexMD5(this.credential)
        console.log(this.userKey.credential)

    },
    // 先判断用户是不是注册（使用登录账号和密码）
    async checkout() {
        const data = await request({ url: "/islogin.controller" })
        const { isLogin } = data.data
        this.isLogin = isLogin
        console.log(this.isLogin)
    },
    // 点击查看密码
    change() {
        this.setData({
            flag: !this.data.flag
        })
    },
    // 用于登录
    async handleLogin() {
        if (this.isLogin == false) {
            if (this.loginmsg.length > 0 && this.credential.length > 0) {
                const res = await request({ url: "/login.controller", data: { userkey: this.userKey } })
                if (res.msg === "登录成功") {
                    await showToast({
                            title: "登录成功"
                        })
                        // wx.navigateTo({ url: '/pages/administrator_page/index?id=' + this.id });
                        // wx.redirectTo({ url: '/pages/administrator_page/index?id=' + this.id });
                        // 删除所有页面栈，重新进去新的页面栈
                    wx.reLaunch({ url: '/pages/administrator_page/index?id=' + this.id });
                } else {
                    await showToast({
                        title: "账号或密码错误，请重试"
                    })
                }
            } else {
                await showToast({
                    title: "请输入账号和密码"
                })
            }
        } else {
            // wx.navigateTo({ url: '/pages/administrator_page/index?id=' + this.id });
            //wx.redirectTo({ url: '/pages/administrator_page/index?id=' + this.id });
            // 删除所有页面栈，重新进去新的页面栈
            wx.reLaunch({ url: '/pages/administrator_page/index?id=' + this.id });
        }
    }

});